
package com.ruoyi.web.controller.lingshui.conpermit;


import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.supervise.domain.BaseConstructionPermit;
import com.ruoyi.supervise.domain.ConPermitFile;
import com.ruoyi.supervise.domain.ConstructionPermit;
import com.ruoyi.supervise.domain.ConstructionPermitUnit;
import com.ruoyi.supervise.domain.MyTest;
import com.ruoyi.supervise.domain.ResponsiblePersonInfo;
import com.ruoyi.supervise.domain.SafetyMeasuresInfo;
import com.ruoyi.supervise.service.IConstructionPermitService;
import com.ruoyi.system.domain.SysUser;

import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: ProjectArchivesController
 * @Description: 施工许可controller
 * @Author wtj
 * @DateTime 2020年3月12日 下午5:10:51
 */
@Controller
@RequestMapping("/conpermit/conpermit")
public class ConstructionPermitController extends BaseController{

	private String prefix = "lingshui/conpermit/applyExamine";
	
	@Autowired
    private IConstructionPermitService constructionPermitService;
	
	@RequiresPermissions("conpermit:conpermit:view")
	@GetMapping()
	public String projectAll()
	{
	    return prefix + "/constructionPermit";
	}
	
	/**
     * 查询核查信息列表
     */
	@RequiresPermissions("conpermit:conpermit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ConstructionPermit constructionPermit)
    {
        startPage();
        // 获取当前的用户信息
        SysUser currentUser = ShiroUtils.getSysUser();
        if(!currentUser.getLoginName().equals("admin") && !currentUser.getLoginName().trim().equals("lingshui")) {
        	constructionPermit.setInChargeDepartmentGuid(currentUser.getDept().getGuid());
        }
        constructionPermit.setState("已上报");
        List<ConstructionPermit> constructionPermitList=constructionPermitService.selectConstructionPermitList(constructionPermit);
        return getDataTable(constructionPermitList);
    }
    
    /**
     * @Title: detail
     * @Description: 查看施工许可申请表
     * @Author wtj
     * @DateTime 2020年3月24日 上午8:57:27
     * @param guid
     * @param mmap
     * @return
     */
   @RequiresPermissions("conpermit:conpermit:apply")
   @GetMapping("/detail/{id}")
   public String detail(@PathVariable("id") String guid,ModelMap mmap)
   {
	   mmap.put("isclose", "false");
	   BaseConstructionPermit bcp = constructionPermitService.selectConsturctionLicenceInfoByGuid(guid);
	   mmap.put("bcp", bcp);
	   ConstructionPermitUnit cpu = constructionPermitService.selectConsturctionLicenceUnit(guid,bcp);
	   mmap.put("cpu", cpu);
	   ResponsiblePersonInfo rpi = new ResponsiblePersonInfo();
	   rpi.setDataGuid(guid);
	   rpi.setUnitType("建设单位");
	   List<ResponsiblePersonInfo> jsLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("jsLits", jsLits);
	   rpi.setUnitType("勘察单位");
	   List<ResponsiblePersonInfo> kcLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("kcLits", kcLits);
	   rpi.setUnitType("设计单位");
	   List<ResponsiblePersonInfo> sjLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("sjLits", sjLits);
	   rpi.setUnitType("施工单位");
	   List<ResponsiblePersonInfo> sgLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("sgLits", sgLits);
	   rpi.setUnitType("监理单位");
	   List<ResponsiblePersonInfo> jlLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("jlLits", jlLits);
	   rpi.setUnitType("检测单位");
	   List<ResponsiblePersonInfo> jcLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("jcLits", jcLits);
	   List<SafetyMeasuresInfo> smiList = constructionPermitService.selectSafetyMeasuresInfoByDataGuid(guid);
	   mmap.put("smiList", smiList);
	   List<ConPermitFile> cpfList = constructionPermitService.selectConPermitFileByObjectId(guid);
	   if(!StringUtils.isEmpty(cpfList)) {
		   for(ConPermitFile cpf:cpfList) {
			   String[] subStr=StrUtil.split(cpf.getAcceName(), "|");
			   if(subStr.length == 4) {
				   cpf.setAcceName(Global.getConfig("ruoyi.bigPlatform")+subStr[1]);
	        	}else {
	        		cpf.setAcceName(" ");
	        	}
		   }
	   }
	   mmap.put("cpfList", cpfList);
	   mmap.put("compactBeginDate", DateUtils.parseDate(bcp.getCompactBeginDate()));
	   mmap.put("compactCompletedData", DateUtils.parseDate(bcp.getCompactCompletedData()));
	   mmap.put("reportTime", DateUtils.parseDate(bcp.getReportTime()));
	   mmap.put("acceptTime", DateUtils.parseDate(bcp.getAcceptTime()));
	   mmap.put("authorizedDate", DateUtils.parseDate(bcp.getAuthorizedDate()));
	   return prefix + "/constructionPermitApply";
   }
   
   /**
    * @Title: edit
    * @Description: 跳转到审批页面
    * @Author wtj
    * @DateTime 2020年3月24日 上午8:57:50
    * @param guid
    * @param mmap
    * @return
    */
   @RequiresPermissions("conpermit:conpermit:apply")
   @GetMapping("/edit/{guid}")
   public String edit(@PathVariable("guid") String guid, ModelMap mmap)
   {
	   mmap.put("isclose", "false");
	   BaseConstructionPermit bcp = constructionPermitService.selectConsturctionLicenceInfoByGuid(guid);
	   mmap.put("bcp", bcp);
	   ConstructionPermitUnit cpu = constructionPermitService.selectConsturctionLicenceUnit(guid,bcp);
	   mmap.put("cpu", cpu);
	   ResponsiblePersonInfo rpi = new ResponsiblePersonInfo();
	   rpi.setDataGuid(guid);
	   rpi.setUnitType("建设单位");
	   List<ResponsiblePersonInfo> jsLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("jsLits", jsLits);
	   rpi.setUnitType("勘察单位");
	   List<ResponsiblePersonInfo> kcLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("kcLits", kcLits);
	   rpi.setUnitType("设计单位");
	   List<ResponsiblePersonInfo> sjLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("sjLits", sjLits);
	   rpi.setUnitType("施工单位");
	   List<ResponsiblePersonInfo> sgLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("sgLits", sgLits);
	   rpi.setUnitType("监理单位");
	   List<ResponsiblePersonInfo> jlLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("jlLits", jlLits);
	   rpi.setUnitType("检测单位");
	   List<ResponsiblePersonInfo> jcLits = constructionPermitService.selectRespPerByUnitTypeAndDataGuid(rpi);
	   mmap.put("jcLits", jcLits);
	   List<SafetyMeasuresInfo> smiList = constructionPermitService.selectSafetyMeasuresInfoByDataGuid(guid);
	   mmap.put("smiList", smiList);
	   List<ConPermitFile> cpfList = constructionPermitService.selectConPermitFileByObjectId(guid);
	   if(!StringUtils.isEmpty(cpfList)) {
		   for(ConPermitFile cpf:cpfList) {
			   String[] subStr=StrUtil.split(cpf.getAcceName(), "|");
			   if(subStr.length == 4) {
				   cpf.setAcceName(Global.getConfig("ruoyi.bigPlatform")+subStr[1]);
	        	}else {
	        		cpf.setAcceName(" ");
	        	}
		   }
	   }
	   mmap.put("cpfList", cpfList);
	   String od = constructionPermitService.selectOperateDescription(guid);
	   mmap.put("od", od);
	   mmap.put("compactBeginDate", DateUtils.parseDate(bcp.getCompactBeginDate()));
	   mmap.put("compactCompletedData", DateUtils.parseDate(bcp.getCompactCompletedData()));
	   mmap.put("reportTime", DateUtils.parseDate(bcp.getReportTime()));
	   mmap.put("acceptTime", DateUtils.parseDate(bcp.getAcceptTime()));
	   mmap.put("authorizedDate", DateUtils.parseDate(bcp.getAuthorizedDate()));
	   return prefix + "/constructionPermitApply";
   }
   
   @PostMapping("/editSave")
   @ResponseBody
   public AjaxResult editSave(@Validated MyTest user)
   {
	return null;
   }
   
   @GetMapping("/detailQua/{id}")
   public String detailQua(@PathVariable("id") String guid,ModelMap mmap)
   {
	   return prefix + "/detailRZ";
   }
   
   @GetMapping("/detailOne/{id}")
   public String detailOne(@PathVariable("id") String guid,ModelMap mmap)
   {
	   return prefix + "/detailZS";
   }
}
